@svizzle/utils/object-[object-object]

Methods

(static) makeMergeAppliedFnMap(fnMap) → {function}

Source:
Since:
  • 0.9.0
See:

Return a function that applies the provided map to the expected object and merges the result to the object. This is useful to add new properties to an object, eventually modifying existing ones by using keys expected to be in the input objects.

Example
> enhancer = makeMergeAppliedFnMap({
	coords: _.collect([_.getKey('lng'), _.getKey('lat')]),
	fullname: _.pipe([
		_.collect([_.getKey('fname'), _.getKey('lname')]),
		_.joinWith(' ')
	]),
	lat: obj => roundTo2(obj.lat),
	lng: obj => roundTo2(obj.lng),
})
> enhancer({
	fname: 'John',
	lat: 2.345434,
	lname: 'Woo',
	lng: 10.3425,
})
{
	coords: [10.3425, 2.345434],
	fname: 'John',
	fullname: 'John Woo',
	lat: 2.35,
	lname: 'Woo',
	lng: 10.34,
}
Parameters:
Name Type Description
fnMap object

a map key/function Any -> Any (applied to the object)

Returns:
  • Object -> Object
Type
function

(static) makeMergeKeyValue(key, object) → {function}

Source:
Since:
  • 0.1.0

Return a function that merges the provided value on the provided key of the expected object

Example
> mergeFooValue = makeMergeKeyValue('foo', {b: -2, c: -3})
> mergeFooValue({
	foo: {a: 1, b: 2},
	bar: {k: 1}
})
{
	foo: {a: 1, b: -2, c: -3},
	bar: {k: 1}
}
> mergeFooValue({
	bar: {k: 1}
})
{
	foo: {b: -2, c: -3},
	bar: {k: 1}
}
Parameters:
Name Type Description
key string

Key where to merge the Value

object object

Value to be merged

Returns:
  • Object -> Object
Type
function

(static) mergeObj(object) → {function}

Source:
Since:
  • 0.1.0

Return a function expecting an object to merge with the input object

Example
> mergeB = mergeObj({b: 2})
> mergeB({a: 1})
{a: 1, b: 2}
> mergeB({a: 1, b: 1})
{a: 1, b: 2}
Parameters:
Name Type Description
object object

Object to be merged to the provided object

Returns:
  • Object -> Object
Type
function

(static) transformPaths(pathToFn) → {function}

Source:
Since:
  • 0.6.0
See:

Return a function that expects an object and applies the functions in the values of the input object to the values of the provided object found in the paths in the correspondent keys. Note that since the provided transforms is an object, paths can be processed only once. However, providing a transform that makes another transform meaningless can generate errors because internally the for..in statement is used to list transforms: because the order of iteration is implementation-dependent, the order of the execution could be unpredictable on old browsers. To apply a specific sequence of transforms, including those modifying the same path multiple times, please see applyTransformsSequence.

Example
> transform = transformPaths({
   'a.a2.a22': _.pipe([Number, Math.sqrt]),
   'a.a3': parseInt,
   'b.b2': parseInt,
 })
> transform({
	a: {
		a1: 'a1',
		a2: {
			a21: 'a21',
			a22: '9',
		},
		a3: '3px',
		a4: '2',
	},
	b: {
		b1: 'b1',
		b2: '4px'
	},
})
{
	a: {
		a1: 'a1',
		a2: {
			a21: 'a21',
			a22: 3,
		},
		a3: 3,
		a4: '2',
	},
	b: {
		b1: 'b1',
		b2: 4
	},
}
> dangerousTransform = transformPaths({
	'a': _.values,     // assuming we have an object in `a`...
	'a.0': x => 2 * x  // ...if this runs first, it could return `2 * undefined = NaN`
 });
Parameters:
Name Type Description
pathToFn object

object with paths as keys and functions as values

Returns:
  • Object -> Object
Type
function

(static) transformValues(fnMap) → {function}

Source:
Since:
  • 0.1.0
See:

Return a function that expects an object and applies the functions in the values of the input object to the correspondent values of the provided object. Can be useful with d3.csvParse, see the example below. Since 0.6.0 it assumes identity for missing keys.

Example
> conversionFn = transformValues({
	name: _.identity,
	a: _.pipe([Number, Math.sqrt]),
	b: Number,
	width: parseFloat
})
> conversionFn({name: 'foo', a: '9', b: '2', width: '10px'})
{name: 'foo', a: 3, b: 2, width: 10}

$ cat baseurl/file.csv
name,a,b,width
foo,9,2,10px
bar,4,4,25px

> d3.csvParse('baseurl/file.csv', conversionFn)
[{name: 'foo', a: 3, b: 2, width: 10}, {name: 'bar', a: 2, b: 4, width: 25}]

> conversionFn = transformValues({
	a: _.pipe([Number, Math.sqrt]),
})
> conversionFn({name: 'foo', a: '9', b: '2', width: '10px'})
{name: 'foo', a: 3, b: '2', width: '10px'}
Parameters:
Name Type Description
fnMap object

object with functions as values

Returns:
  • Object -> Object
Type
function

(static) updateKeys() → {function}

Source:
Since:
  • 0.16.0

Return a function that expects an object and applies the provided updater function to the values correspondent to the provided keys, leaving the other properties unchanged.

Example
> update = updateKeys({
	keys: ['a', 'k', 'm'],
	updater: x => x * 2
});
> update({a: 1, b: 2, d: 4, k: 7, m: 2})
{a: 2, b: 2, d: 4, k: 14, m: 4}
> update({a: 1, b: 2, d: 4})
{a: 2, b: 2, d: 4}
> update({b: 2, d: 4})
{b: 2, d: 4}
Parameters:
Type Description
object

{keys: Array, updater: Any -> Any}

Returns:
  • Object -> Object
Type
function